package assists
	
{
	
	import flash.display.Bitmap;
	
	import flash.display.BitmapData;
	
	import flash.display.Sprite;
	
	import flash.geom.Point;
	
	import flash.geom.Rectangle;
	
	public class Scale9Grid extends Sprite
		
	{
		
		private var _over:Bitmap;
		
		private var _graphics:BitmapData;
		
		private var _cornerWidth:Number;
		
		private var _cornerHeight:Number;
		
		private var _slicingGrapincs:Array;
		
		private var _width:Number;
		
		private var _height:Number;
		
		/**
		 * 
		 * @param bitmapData
		 * @param cornerWidth  9宫格每个小方框的长宽
		 * @param cornerHeight
		 * 
		 */		
		public function Scale9Grid(bitmapData:BitmapData = null, cornerWidth:Number = 0, cornerHeight:Number = 0) 
			
		{
			
			_graphics = bitmapData;
			
			this._cornerHeight = cornerHeight;
			
			this._cornerWidth = cornerWidth;
			
			_slicingGrapincs = new Array();
			
			if (_graphics != null) 
				
			{
				
				_width = _graphics.width;
				
				_height = _graphics.height;
				
				checkParameter();
				
				splicegraphic();
				
				setSize(_width, _height);
				
			}
			
		}
		
		override public function set height(height:Number):void
			
		{
			if(height <=0 ) height = 1;
			_height = height;
			
			setSize(_width, _height);
			
		}
		
		override public function get height():Number
			
		{
			
			return _height;
			
		}
		
		override public function set width(width:Number):void
			
		{
			if(width <= 1) width = 1;
			_width = width;
			
			setSize(_width, height);
			
		}
		
		override public function get width():Number
			
		{
			
			return _width;
			
		}
		
		public function setSize(width:Number, height:Number):void
			
		{
			
			if(_over && _over.parent)_over.parent.removeChild(_over);
			
			
			var bitmap:Bitmap;
			
			
			bitmap = getSlice(UP_LEFT);
			
			bitmap.x = 0;
			
			bitmap.y = 0;
			
			bitmap.visible = true;
			
			
			bitmap = getSlice(UP);
			
			bitmap.x = _cornerWidth;
			
			bitmap.y = 0;
			
			bitmap.width = width - _cornerWidth * 2;
			
			bitmap.visible = true;
			
			
			bitmap = getSlice(UP_RIGHT);
			
			bitmap.x = width - _cornerWidth;
			
			bitmap.y = 0;
			
			bitmap.visible = true;
			
			
			bitmap = getSlice(LEFT);
			
			bitmap.x = 0;
			
			bitmap.y = _cornerHeight;
			
			bitmap.height = height - _cornerHeight * 2 + 2;
			
			bitmap.visible = true;
			
			
			bitmap = getSlice(CENTER);
			
			bitmap.x = _cornerWidth;
			
			bitmap.y = _cornerHeight;
			
			bitmap.width = width - _cornerWidth * 2;
			
			bitmap.height = height - _cornerHeight * 2 + 2;
			
			bitmap.visible = true;
			
			
			bitmap = getSlice(RIGHT);
			
			bitmap.x = width - _cornerWidth;
			
			bitmap.y = _cornerHeight;
			
			bitmap.height = height - _cornerHeight * 2 + 2;
			
			bitmap.visible = true;
			
			
			bitmap = getSlice(DOWN_LEFT);
			
			bitmap.x = 0;
			
			bitmap.y = height - _cornerHeight;
			
			bitmap.visible = true;
			
			
			bitmap = getSlice(DOWN);
			
			bitmap.x = _cornerWidth;
			
			bitmap.y = height - _cornerHeight ;
			
			bitmap.width = width - _cornerWidth * 2;
			
			bitmap.visible = true;
			
			
			bitmap = getSlice(DOWN_RIGHT);
			
			bitmap.x = width - _cornerWidth;
			
			bitmap.y = height - _cornerHeight;
			
			bitmap.visible = true;
			
			
			var bd:BitmapData = new BitmapData(width,height,true,0x00000000);
			
			bd.draw(this);
			
			_over = new Bitmap(bd);
			
			addChild(_over);
			
			
			bitmap = getSlice(UP_LEFT);
			
			bitmap.visible = false;
			
			
			bitmap = getSlice(UP);
			
			bitmap.visible = false;
			
			
			bitmap = getSlice(UP_RIGHT);
			
			bitmap.visible = false;
			
			
			bitmap = getSlice(LEFT);
			
			bitmap.visible = false;
			
			
			bitmap = getSlice(CENTER);
			
			bitmap.visible = false;
			
			
			bitmap = getSlice(RIGHT);
			
			bitmap.visible = false;
			
			
			bitmap = getSlice(DOWN_LEFT);
			
			bitmap.visible = false;
			
			
			bitmap = getSlice(DOWN);
			
			bitmap.visible = false;
			
			
			bitmap = getSlice(DOWN_RIGHT);
			
			bitmap.visible = false;
			
		}
		
		private function splicegraphic():void
			
		{
			
			var bitmapData:BitmapData;
			
			var rectangle:Rectangle;
			
			var point:Point = new Point();
			
			var tWidth:Number;
			
			var tHeight:Number;
			
			
			//UP_LEFT corner
			
			rectangle = new Rectangle(0, 0, _cornerWidth, _cornerHeight);
			
			bitmapData = new BitmapData(_cornerWidth, _cornerHeight);
			
			bitmapData.copyPixels(_graphics, rectangle, point);
			
			_slicingGrapincs.push(new Bitmap(bitmapData));
			
			
			//UP bar
			
			rectangle = new Rectangle(_cornerWidth, 0, _width - _cornerWidth * 2, _cornerHeight);
			
			bitmapData = new BitmapData(_width - _cornerWidth * 2, _cornerHeight);
			
			bitmapData.copyPixels(_graphics, rectangle, point);
			
			_slicingGrapincs.push(new Bitmap(bitmapData));
			
			
			//UP_RIGHT corner
			
			rectangle = new Rectangle(_width - _cornerWidth, 0, _cornerWidth, _cornerHeight);
			
			bitmapData = new BitmapData(_cornerWidth, _cornerHeight);
			
			bitmapData.copyPixels(_graphics, rectangle, point);
			
			_slicingGrapincs.push(new Bitmap(bitmapData));
			
			
			//LEFT bar
			
			rectangle = new Rectangle(0, _cornerHeight, _cornerWidth, _height - _cornerHeight * 2);
			
			bitmapData = new BitmapData(_cornerWidth, _height - _cornerHeight * 2);
			
			bitmapData.copyPixels(_graphics, rectangle, point);
			
			_slicingGrapincs.push(new Bitmap(bitmapData));
			
			
			//CENTER
			
			rectangle = new Rectangle(_cornerWidth, _cornerHeight, _width - _cornerWidth * 2, _height - _cornerHeight * 2);
			
			bitmapData = new BitmapData(_width - _cornerWidth * 2, _height - _cornerHeight * 2);
			
			bitmapData.copyPixels(_graphics, rectangle, point);
			
			_slicingGrapincs.push(new Bitmap(bitmapData));
			
			
			//RIGHT bar
			
			rectangle = new Rectangle(_width - _cornerWidth, _cornerHeight, _cornerWidth, _height - _cornerHeight * 2);
			
			bitmapData = new BitmapData(_cornerWidth, _height - _cornerHeight * 2);
			
			bitmapData.copyPixels(_graphics, rectangle, point);
			
			_slicingGrapincs.push(new Bitmap(bitmapData));
			
			
			//DOWN_LEFT bar
			
			rectangle = new Rectangle(0, _height - _cornerHeight, _cornerWidth, _cornerHeight);
			
			bitmapData = new BitmapData(_cornerWidth, _cornerHeight);
			
			bitmapData.copyPixels(_graphics, rectangle, point);
			
			_slicingGrapincs.push(new Bitmap(bitmapData));
			
			
			//DOWN bar
			
			rectangle = new Rectangle(_cornerWidth, _height - _cornerHeight, _width - _cornerWidth * 2, _cornerHeight);
			
			bitmapData = new BitmapData(_width - _cornerWidth * 2, _cornerHeight);
			
			bitmapData.copyPixels(_graphics, rectangle, point);
			
			_slicingGrapincs.push(new Bitmap(bitmapData));
			
			
			//DOWN_RIGHT bar
			
			rectangle = new Rectangle(_width - _cornerWidth, _height - _cornerHeight, _cornerWidth, _cornerHeight);
			
			bitmapData = new BitmapData(_cornerWidth, _cornerHeight);
			
			bitmapData.copyPixels(_graphics, rectangle, point);
			
			_slicingGrapincs.push(new Bitmap(bitmapData));
			
			
			for (var i:int = _slicingGrapincs.length - 1; i >= 0; -- i) 
				
			{
				
				addChild(_slicingGrapincs[i]);
				
			}
			
		}
		
		
		private const UP_LEFT:uint = 0;
		
		private const UP:uint = 1;
		
		private const UP_RIGHT:uint = 2;
		
		private const LEFT:uint = 3;
		
		private const CENTER:uint = 4;
		
		private const RIGHT:uint = 5;
		
		private const DOWN_LEFT:uint= 6;
		
		private const DOWN:uint = 7;
		
		private const DOWN_RIGHT:uint = 8;
		
		private function getSlice(sign:uint):Bitmap
			
		{
			
			return _slicingGrapincs[sign];
			
		}
		
		
		private function checkParameter():void
			
		{
			
			if (_cornerWidth * 2 >= _width) 
				
			{
				
				_cornerWidth = _width / 2 - 1;
				
			}
			
			if (_cornerHeight * 2 >= _height) 
				
			{
				
				_cornerHeight = _height / 2 - 1;
				
			}
			
		}
		
	}
	
	
}